<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\controller;

class AdminSalesman extends \app\AdminRest
{
    protected $model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\massage\model\Salesman();
    }
    public function salesmanList()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        if($this->_user["is_admin"] == 0) {
            $admin_model = new \app\massage\model\Admin();
            $admin_arr = $admin_model->where("id", "in", $this->admin_arr)->where(["salesman_auth" => 1])->column("id");
            $dis[] = ["a.admin_id", "in", $admin_arr];
        }
        if(!empty($input["admin_id"])) {
            $dis[] = ["a.admin_id", "=", $input["admin_id"]];
        }
        if(!empty($input["status"])) {
            $dis[] = ["a.status", "=", $input["status"]];
        } else {
            $dis[] = ["a.status", ">", -1];
        }
        if(!empty($input["salesman_id"])) {
            $dis[] = ["a.id", "<>", $input["salesman_id"]];
        }
        if(!empty($input["start_time"]) && !empty($input["end_time"])) {
            $dis[] = ["a.create_time", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        $where = [];
        if(!empty($input["name"])) {
            $where[] = ["a.user_name", "like", "%" . $input["name"] . "%"];
            $where[] = ["a.phone", "like", "%" . $input["name"] . "%"];
        }
        $data = $this->model->adminDataList($dis, $where, $input["limit"]);
        $list = ["all", "ing", "pass", "4" => "nopass"];
        foreach ($list as $key => $value) {
            $dis = ["uniacid" => $this->_uniacid];
            if(!empty($key)) {
                $dis["status"] = $key;
            }
            $where = [];
            if($this->_user["is_admin"] == 0) {
                $where[] = ["admin_id", "in", $this->admin_arr];
            }
            $data[$value] = $this->model->where($dis)->where($where)->where("status", ">", -1)->count();
        }
        return $this->success($data);
    }
    public function salesmanInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $data = $this->model->dataInfo($dis);
        $admin_model = new \app\massage\model\Admin();
        $user_modle = new \app\massage\model\User();
        $admin = $admin_model->dataInfo(["id" => $data["admin_id"], "status" => 1]);
        if(!empty($admin["salesman_auth"])) {
            $data["admin_name"] = $admin["agent_name"];
        } else {
            $data["admin_id"] = 0;
        }
        $data["nickName"] = $user_modle->where(["id" => $data["user_id"], "status" => 1])->value("nickName");
        if($data["balance"] < 0) {
            $data["balance"] = getConfigSetting($this->_uniacid, "salesman_balance");
        }
        return $this->success($data);
    }
    public function checkSalesman()
    {
        $input = $this->_input;
        $diss = ["id" => $input["id"]];
        $salesman = $this->model->dataInfo($diss);
        if(isset($input["status"]) && in_array($input["status"], [2, 4]) && $salesman["status"] == 1) {
            $input["sh_time"] = time();
        }
        if(isset($input["status"]) && $input["status"] == -1) {
            if(0 < $salesman["cash"]) {
                $this->errorMsg("业务员还有佣金未提现");
            }
            $dis = ["top_id" => $input["id"], "status" => 1, "type" => 12];
            $cash_model = new \app\massage\model\Commission();
            $cash = $cash_model->dataInfo($dis);
            if(!empty($cash)) {
                $this->errorMsg("业务员还有佣金未到账");
            }
            $dis = ["coach_id" => $input["id"], "status" => 1, "type" => 6];
            $wallet_model = new \app\massage\model\Wallet();
            $wallet = $wallet_model->dataInfo($dis);
            if(!empty($wallet)) {
                $this->errorMsg("业务员还有提现未处理");
            }
        }
        $res = $this->model->dataUpdate($diss, $input);
        return $this->success($res);
    }
    public function salesmanDataList()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        if($this->_user["is_admin"] == 0) {
            $dis[] = ["a.admin_id", "in", $this->admin_arr];
        }
        $dis[] = ["a.status", "=", 2];
        if(!empty($input["start_time"]) && !empty($input["end_time"])) {
            $dis[] = ["a.sh_time", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        $where = [];
        if(!empty($input["name"])) {
            $where[] = ["a.user_name", "like", "%" . $input["name"] . "%"];
            $where[] = ["a.phone", "like", "%" . $input["name"] . "%"];
            $where[] = ["b.nickName", "like", "%" . $input["name"] . "%"];
        }
        $data = $this->model->adminDataList($dis, $where, $input["limit"]);
        if(!empty($data["data"])) {
            $wallet_model = new \app\massage\model\Wallet();
            foreach ($data["data"] as &$v) {
                $v["wallet_price"] = $wallet_model->where(["coach_id" => $v["id"], "type" => 6])->where("status", "in", [1, 2])->sum("apply_price");
                $v["order_price"] = $this->model->salesmanOrderPrice($v["id"]);
                $v["balance"] = floatval($v["balance"]);
                $v["wallet_price"] = round($v["wallet_price"], 2);
            }
        }
        return $this->success($data);
    }
    public function setSalesmanBalance()
    {
        $input = $this->_input;
        $res = $this->model->where("id", "in", $input["id"])->update(["balance" => $input["balance"]]);
        return $this->success($res);
    }
    public function delSalesmanBalance()
    {
        $input = $this->_input;
        $res = $this->model->where("id", "in", $input["id"])->update(["balance" => -1]);
        return $this->success($res);
    }
    public function addSalesman()
    {
        $input = $this->_input;
        $salesman_model = new \app\massage\model\Salesman();
        $dis[] = ["user_id", "=", $input["user_id"]];
        $dis[] = ["status", ">", -1];
        $info = $salesman_model->dataInfo($dis);
        if(!empty($info) && in_array($info["status"], [1, 2, 3])) {
            $this->errorMsg("你已经申请过分销员了");
        }
        $admin_id = $this->_user["is_admin"] == 0 ? $this->_user["id"] : 0;
        if(!empty($input["admin_id"])) {
            $admin_id = $input["admin_id"];
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $input["user_id"], "admin_id" => $admin_id, "phone" => $input["phone"], "user_name" => $input["user_name"], "true_user_name" => !empty($input["true_user_name"]) ? $input["true_user_name"] : $input["user_name"], "status" => 2, "sh_time" => time()];
        $res = $salesman_model->dataAdd($insert);
        return $this->success($res);
    }
    public function noSalesmanUserList()
    {
        $input = $this->_param;
        if($this->_user["is_admin"] == 0 && empty($input["nickName"])) {
            $where[] = ["id", "=", -1];
        }
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "in", [0, 1, 2, 3]];
        $user_id = $this->model->where($dis)->column("user_id");
        $where1 = [];
        if(!empty($input["nickName"])) {
            if($this->_user["is_admin"] == 0) {
                $where1[] = ["nickName", "=", $input["nickName"]];
                $where1[] = ["phone", "=", $input["nickName"]];
            } else {
                $where1[] = ["nickName", "like", "%" . $input["nickName"] . "%"];
                $where1[] = ["phone", "like", "%" . $input["nickName"] . "%"];
            }
        }
        $user_model = new \app\massage\model\User();
        $where[] = ["uniacid", "=", $this->_uniacid];
        $where[] = ["id", "not in", $user_id];
        $where[] = ["status", "=", 1];
        $list = $user_model->dataList($where, $input["limit"], $where1);
        return $this->success($list);
    }
}

?>